package cc.ug_project.rollcall.Activities

import android.os.Build
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import cc.ug_project.rollcall.Core.Observer.observable
import cc.ug_project.rollcall.Fragments.RollCallFragment
import cc.ug_project.rollcall.Fragments.StatisticsFragment
import cc.ug_project.rollcall.Fragments.UserCenterFragment
import cc.ug_project.rollcall.Fragments.UserManageFragment
import cc.ug_project.rollcall.R
import com.ug_project.adapters.createFragmentPagerAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val pages by lazy { arrayOf(
            RollCallFragment(),
            UserManageFragment(),
            StatisticsFragment(),
            UserCenterFragment()
    ) }

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_RollCall -> {
                viewPager.currentItem = 0
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_UserManager -> {
                viewPager.currentItem = 1
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_Statistics -> {
                viewPager.currentItem = 2
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_UserCenter -> {
                viewPager.currentItem = 3
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(Build.VERSION.SDK_INT>=21){
            supportActionBar?.setElevation(0f);
        }

        viewPager.adapter = createFragmentPagerAdapter(supportFragmentManager){
            this.GetItem { pages[it] }
            this.GetCount { pages.size }
        }

        viewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                navigation.selectedItemId = when (position){
                    0 -> R.id.navigation_RollCall
                    1 -> R.id.navigation_UserManager
                    2 -> R.id.navigation_Statistics
                    3 -> R.id.navigation_UserCenter
                    else -> 0
                }
            }

        })

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
    }
}
